import csv

# 打开输入文件和输出文件
with open('data1.csv', 'r',encoding='utf-8') as csv_file, open('output.csv', 'w', newline='',encoding='utf-8') as output_file:
    
    # 读取CSV文件的内容
    reader = csv.DictReader(csv_file)

    # 设定输出文件的列名
    fieldnames = ['Subject', 'Predicate', 'Object']
    
    # 创建csv.DictWriter对象并设定列名
    writer = csv.DictWriter(output_file, fieldnames=fieldnames)
    
    # 写入输出文件的列名
    writer.writeheader()

    # 遍历读取到的所有行
    for row in reader:
        name = row['name'] # 获取主语
        for key, value in row.items():
            if (key == 'medium' or key=='classification' or key=='region' or key=='museum')and value.strip() != '': # 只关心非空的键值对
                writer.writerow({'Subject': name, 'Predicate': key, 'Object': value})